$space-scales:0, 0.25, 0.5, 1, 1.5, 2, 3, 4, 5; 
$padding-directions: (
    '': 'padding',
    't': 'padding-top',
    'r': 'padding-right',
    'l': 'padding-left',
    'b': 'padding-bottom',
    'x': ('padding-left','padding-right'),
    'y': ('padding-top','padding-bottom')
);

$margin-directions: (
    '': 'margin',
    't': 'margin-top',
    'r': 'margin-right',
    'l': 'margin-left',
    'b': 'margin-bottom',
    'x': ('margin-left','margin-right'),
    'y': ('margin-top','margin-bottom')
);

@mixin spacing-class($name, $directions, $scales, $auto: false, $negative: false) {
    @each $dir, $prop in $directions {
        @for $i from 0 through length($scales) - 1 {
            .#{$name}#{$dir}-#{$i} {
                @each $propName in $prop {
                    #{$propName}: $spacer * nth($scales, $i + 1) !important;
                }
            }
        }

        @if ($negative) {
            @for $i from 1 through length($scales) - 1 {
                .-#{$name}#{$dir}-#{$i} {
                    @each $propName in $prop {
                        #{$propName}: -1 * $spacer * nth($scales, $i + 1) !important;
                    }
                }
            }
        }

        @if ($auto) {
            .#{$name}#{$dir}-auto {
                @each $propName in $prop {
                    #{$propName}: auto !important;
                }
            }
        } 
    }

    @each $key, $val in $breakpoints {
        @media screen and (min-width: #{$val}) {
            @each $dir, $prop in $directions {
                @for $i from 0 through length($scales) - 1 {
                    .#{$key + $separator}#{$name}#{$dir}-#{$i} {
                        @each $propName in $prop {
                            #{$propName}: $spacer * nth($scales, $i + 1) !important;;
                        }
                    }
                }

                @if ($negative) {
                    @for $i from 1 through length($scales) - 1 {
                        .#{$key + $separator}-#{$name}#{$dir}-#{$i} {
                            @each $propName in $prop {
                                #{$propName}: -1 * $spacer * nth($scales, $i + 1) !important;
                            }
                        }
                    }
                }

                @if ($auto) {
                    .#{$key + $separator}#{$name}#{$dir}-auto {
                        @each $propName in $prop {
                            #{$propName}: auto !important;;
                        }
                    }
                }
            }
        }
    }
}

@include spacing-class('p', $padding-directions, $space-scales);
@include spacing-class('m', $margin-directions, $space-scales, true, true);